package jtk.project4.fleet.screen.addGeneralExpense;

/*
* Author : Rio Dermawan 
* Date Created : 1-April-2011
* Date Modified : 6-April-2011
* Version : 1.1
* */


import java.awt.FlowLayout;
import java.util.List;

import jtk.project4.fleet.action.CancelAction;
import jtk.project4.fleet.action.CancelAddGeneralExpenseAction;
import jtk.project4.fleet.action.SaveAddGeneralExpenseAction;
import jtk.project4.fleet.action.SubmitAction;
import jtk.project4.fleet.domain.MiscExp;
import jtk.project4.fleet.event.AddGeneralExpenseEvent;
import jtk.project4.fleet.event.AddGeneralExpenseEvent;
import jtk.project4.fleet.field.AddGeneralExpenseField;
import jtk.project4.fleet.field.AddGeneralExpenseField;


import nl.coderight.jazz.form.FormHeader;
import nl.coderight.jazz.form.FormView;
import nl.coderight.jazz.form.control.GroupControl;
import nl.coderight.jazz.form.field.button.PushButton;
import nl.coderight.jazz.form.validation.ValidationError;
	
public class AddGeneralExpenseView extends FormView {
	private PushButton saveButton;
	private PushButton cancelButton;
	private SubmitAction submitAction;
	private SubmitAction submitAction2;
	private CancelAction cancelAction;
	private MiscExp miscExp;
	
	public void handleEvent(AddGeneralExpenseEvent evt) {
		switch (evt.getType()) {
		case CANCEL:
			close();
			break;
			
		case SAVE:
			// check for validation errors
			List<ValidationError> errors = validate();
			if (!errors.isEmpty()) {
				// there are validation errors so do not submit...
				errors.get(0).getField().focus();
				break;
			} else {
				// submit form and close view
				//submit();
				close();
				// fall-through!
			}
			
		default:
			// propagate event to controller
			propagateEvent(evt);
			break;
		}
	}

	@Override
	protected void init() {
		createFields();
		createLayout();	
		
	}
	
	private void createAction(){
		submitAction = new SubmitAction();
		submitAction2 = new SubmitAction();
		cancelAction = new CancelAction();
		setOnChangeAction(submitAction);
	}

	private GroupControl createButtons() {
		GroupControl buttons = new GroupControl();
		buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
		buttons.addField(saveButton);
		buttons.addField(cancelButton);
		return buttons;
	}
	
	private GroupControl createForm() {
		GroupControl form = new GroupControl();
		form.addField(new AddGeneralExpenseField("miscExp"), NORTH);
		form.addField(createButtons(), SOUTH);		
		return form;
	}
	
	private void createLayout() {
		addField(createForm());
	}
	
	private void createFields(){
		saveButton = new PushButton(new SaveAddGeneralExpenseAction());
		cancelButton = new PushButton(new CancelAddGeneralExpenseAction());
	}
}
